<!--
    资料上传 弹窗 公共插件

    参数：
    modal       弹窗开关
    modalTitle  弹窗title
    param       参数

    方法:
    closeModal          关闭弹窗
    formValidate        文件上传验证  传[funcName] 看 98 行
 -->

<template>
    <div>
        <template v-if="isPreview">
            <Modal
                width="800px"
                v-model="isModalOpen"
                title="文件预览"
                :mask-closable="false"
                @on-visible-change="changeModalState"
                :styles="{top: '30px'}"
                draggable
                footer-hide
            >
                <FilePreview
                    ref="filePreview"
                    :isInit="canUpload"
                    :uploadParam="param"
                    :asyncSaveFile="asyncSaveFile"
                    @asyncSaveFileFn="asyncSaveFileFn"
                ></FilePreview>
            </Modal>
        </template>
        <template v-else>
            <Modal
                width="1110px"
                v-model="isModalOpen"
                title="资料上传"
                :mask-closable="false"
                @on-visible-change="changeModalState"
                :styles="{top: '30px'}"
            >
                <FileOperation
                    ref="fileOperation"
                    :isInt="canUpload"
                    :uploadParam="param"
                    :asyncSaveFile="asyncSaveFile"
                    @asyncSaveFileFn="asyncSaveFileFn"
                    :path="path"
                ></FileOperation>
            </Modal>
        </template>
    </div>
</template>

<script>
import FileOperation from "_p/basic/pages/image/upload-file/fileOperation";
import FilePreview from "_p/basic/pages/image/upload-file/contractFile/preview";
import config from "@/config"
export default {
    name: 'UploadFileModal',
    components: {
        FileOperation,
        FilePreview,
    },
    props: {
        isPreview: {
            type: Boolean,
            default: false,
        },
        modal: {
            type: Boolean,
            required: true
        },

        modalTitle: {
            type: String,
        },
        param: {
            type: Object,
            required: true,
            default: () => {
                return {
                    busiNo: "",
                    belongNo: "",
                    busiType: "",
                    busiData: {},
                    busiNodes: ''
                }
            }
        }

    },
    data() {
        return {
            canUpload: false,
            isModalOpen: this.modal,
            loading: false,
            path: config.getProjectInfo().componentRequestUri.template,
            // uploadParam: {
            //     busiNo: "", //业务编号 (案件编号)
            //     belongNo: "", // 合同号
            //     busiType: "", // 大类
            //     busiData: {} //规则
            //     busiNodes:'' // 业务节点
            // },
            // uploadParam: this.param
            asyncSaveFile: true,
            parentFuncName: '',
        }
    },
    watch: {
        modal: function (newVal) {
            console.log('this.param', this.param, newVal);
            if (newVal) {
                this.isModalOpen = newVal;
            }
            this.canUpload = newVal
        }
    },
    created() {
        // this.modalTitle = this.isPreview ? '文件预览' : '资料上传'
    },
    methods: {
        //  弹窗切换
        changeModalState(bool) {
            if (!bool) this.$emit("closeModal");
        },
        getFileList() {
            if (this.isPreview) {
                return this.$refs.filePreview.getFileList()
            } else {
                return this.$refs.fileOperation.getFileList()
            }
        },
        /*
          * formData 数据验证 验证通过后 将数据传回父级处理  (保存/提交/审核 通用)
          * @param {String} funcName 回传给父级的方法 (注意父级需要传方法)
          * @param {Object} [data]   父级带来的数据
          */
        formValidate(funcName, data) {
            this.parentFuncName = funcName;

            // if (this.asyncSaveFile && !this.isPreview) {
            // this.$refs.fileOperation.formValidate('asyncSaveFileFn', data)
            // } else {
            this.asyncSaveFileFn(data)
            // }
        },
        asyncSaveFileFn(data) {
            this.$emit(this.parentFuncName, data)
        }
    }
}
</script>

<style>
</style>
